package com.xframe.domain.role;

import com.xframe.domain.BaseModel;
import com.xframe.domain.emnus.XDelFlag;
import com.xframe.xdal.core.DataBaseAccess;
import com.xframe.xdal.core.annotation.*;
import com.xframe.xdal.core.constant.AutoFillType;
import com.xframe.xdal.core.constant.DataSourceTypeEnums;
import com.xframe.xdal.core.constant.ErrorCode;
import com.xframe.xdal.core.constant.ErrorMsg;
import com.xframe.xdal.core.exception.DalFrameException;
import com.xframe.xdal.core.model.ConditionValue;
import com.xframe.xdal.core.model.ConditionValueCollection;
import com.xframe.xdal.core.model.mapping.TableInfoBuilder;
import com.xframe.xdal.core.tran.XTransaction;
import com.xframe.xdal.core.util.XDateUtil;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 角色表
 * @ClassName: SysRole
 * @Author: Lean.Liu
 * @CreateTime: 2023/4/3 16:43
 **/
@Log4j2
@Data
@Accessors(chain=true)
@XTable(value = "SYS_ROLE", label = "角色表")
@XSelectItem(keyName = "roleId",codeName = "roleCode",textName = "roleName")
public class SysRole extends BaseModel {

    /**
     * 角色ID
     */
    @AutoFill(AutoFillType.Snowflake)
    @XColumn(name = "ROLE_ID", primaryKeyFlag = true,desc = "角色ID")
    private Long roleId;

    /**
     * 角色编码
     */
    @XColumn(name = "ROLE_CODE",desc = "角色编码",uniqueFlag = true)
    private String roleCode;

    /**
     * 角色名称
     */
    @XColumn(name = "ROLE_NAME",desc = "角色名称")
    private String roleName;

    /**
     * 角色说明
     */
    @XColumn(name = "ROLE_DESC",desc = "角色说明")
    private String roleDesc;


    /**
     * 授权菜单
     */
    @XTransaction
    @XAction(code = "permission",desc = "授权菜单")
    public Object permission(@XActionParameter(name = "ids") List<String> ids,IUserCredentials userCredentials){
        log.info("call Role.permission");
        try {
            log.info(ids);
            if(ids == null || ids.size() == 0){
                new DalFrameException(ErrorCode.ARGS_ERROR, ErrorMsg.ARGS_ERROR_MSG);
            }
            ConditionValueCollection conditionValueCollection = new ConditionValueCollection();
            conditionValueCollection.add(new ConditionValue("roleId",this.roleId));
            Integer delCount = DataBaseAccess.getInstance().deleteMany(TableInfoBuilder.BuildTableInfoByAnnotation(SysMenuRole.class),conditionValueCollection);
            log.info(delCount);
            List<SysMenuRole> menuRoles = new ArrayList<>();
            for(String id : ids){
                SysMenuRole sysMenuRole = new SysMenuRole();
                sysMenuRole.setMenuId(Long.valueOf(id));
                sysMenuRole.setRoleId(this.roleId);
                sysMenuRole.setDelFlag(XDelFlag.getDefFlag());
                sysMenuRole.setBaseAttrVal(userCredentials);
                menuRoles.add(sysMenuRole);
            }
            DataBaseAccess.getInstance().batchCreate(SysMenuRole.class,menuRoles);
            return "OK";
        } catch (DalFrameException exception) {
            throw exception;
        }
    }

}
